package net.thumbtack.onepm.tests;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import java.lang.String;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.fail;

/**
 * Created with IntelliJ IDEA.
 * User: efrolova
 * Date: 01.10.12
 * Time: 11:11
 * To change this template use File | Settings | File Templates.
 */
public class TestBase {
    static WebDriver driver;
    static String baseUrl;
    private static StringBuffer verificationErrors = new StringBuffer();


  protected String FirstName="NameNew";
    protected String lastName="LName1";
  protected String EmailCompany="mail.ru";
    protected String sex="male";
    protected String position="president";
    protected String letterCity="a";
    protected String AdminEmail="user@thumbtack.net";
    protected String PasswordAd="password";
    protected String textMessage="hellooooooooo";
    protected String idCandidate="7681692775288553680";
     //  public static Object User=new Object();
   //   {

//}


    @BeforeClass
    public static void setUp() throws Exception {
       driver = new FirefoxDriver();

      //  WebDriver driver = new RemoteWebDriver(
       //         new URL(""), DesiredCapabilities.firefox());
        baseUrl = "http://82.200.2.19";
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    }


       @AfterClass
       public static void tearDown() throws Exception {
           driver.quit();
           String verificationErrorString = verificationErrors.toString();
           if (!"".equals(verificationErrorString)) {
               fail(verificationErrorString);
           }
       }




    protected static boolean isElementPresent(By by) {
        try {
            TestBase.driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    protected void fillFormSignUp(UserData userData) throws InterruptedException {
        TestBase.driver.findElement(By.id("firstName")).clear();
        TestBase.driver.findElement(By.id("firstName")).sendKeys(userData.getFirstName());
        TestBase.driver.findElement(By.id("lastName")).clear();
        TestBase.driver.findElement(By.id("lastName")).sendKeys(userData.getLastName());

        new Select(driver.findElement(By.id("birthMonth"))).selectByVisibleText("Jul");
        driver.findElement(By.cssSelector("option[value=\"7\"]")).click();
        new Select(driver.findElement(By.id("birthDay"))).selectByVisibleText("4");
        new Select(driver.findElement(By.id("birthYear"))).selectByVisibleText("1990");
        new Select(TestBase.driver.findElement(By.id("type"))).selectByVisibleText("Working");
        TestBase.driver.findElement(By.id("place")).clear();
        TestBase.driver.findElement(By.id("place")).sendKeys("place");
        TestBase.driver.findElement(By.id("position")).clear();
        TestBase.driver.findElement(By.id("position")).sendKeys(userData.getPosition());
        new Select(driver.findElement(By.id("country"))).selectByVisibleText("Canada");
        driver.findElement(By.cssSelector("option[value=\"39\"]")).click();
        new Select(driver.findElement(By.id("region"))).selectByVisibleText("Alberta");
        TestBase.driver.findElement(By.id("city")).clear();
        TestBase.driver.findElement(By.id("city")).sendKeys(userData.getLetterCity());
        Thread.sleep(3000);
        TestBase.driver.findElement(By.id("city")).sendKeys(Keys.ARROW_DOWN);
        TestBase.driver.findElement(By.id("city")).sendKeys(Keys.ARROW_DOWN);
        TestBase.driver.findElement(By.id("city")).sendKeys(Keys.ENTER);

    }
    protected void fillFormAdm(Admin admin)  throws InterruptedException{
        driver.findElement(By.id("email")).clear();
        driver.findElement(By.id("email")).sendKeys(admin.getEmail());
        driver.findElement(By.id("password")).clear();
        driver.findElement(By.id("password")).sendKeys(admin.getPasswordAd());
        driver.findElement(By.name("submit")).click();
    }
}
